package org.contextinferenceengine;

import java.util.ArrayList;
import java.util.List;

import org.contextinferenceengine.location.LocationContainer;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.location.Location;
import android.util.Log;

public class DatabaseHelper {

	private static final String DATABASE_NAME = "contextInference.db";
	private static final int DATABASE_VERSION = 4;
	private static final String TABLE_NAME = "locations";

	private Context context;
	private SQLiteDatabase db;

	
	
	private SQLiteStatement insertStmt;
	private static final String INSERT = "insert into " + TABLE_NAME
			+ "(timestamp, lat, lon, alt, accuracy, provider, address) values (?,?,?,?,?,?,?)";

	public DatabaseHelper(Context context) {
		this.context = context;
		OpenHelper openHelper = new OpenHelper(this.context);
		this.db = openHelper.getWritableDatabase();
		this.insertStmt = this.db.compileStatement(INSERT);
		

	}

	public long insertLocation(long timestamp, double lat, double lon, double alt, float accuracy, String provider, String address) {
		this.insertStmt.bindLong(1, timestamp);
		this.insertStmt.bindDouble(2, lat);
		this.insertStmt.bindDouble(3, lon);
		this.insertStmt.bindDouble(4, alt);
		this.insertStmt.bindDouble(5, accuracy);
		this.insertStmt.bindString(6, provider);
		this.insertStmt.bindString(7, address);
		
		
		return this.insertStmt.executeInsert();
	}

	public void deleteAll() {
		this.db.delete(TABLE_NAME, null, null);
	}

	public List<LocationContainer> selectAll() {
		List<LocationContainer> list = new ArrayList<LocationContainer>();
		Cursor cursor = this.db.query(TABLE_NAME, new String[] { "timestamp", "lat", "lon", "alt", "accuracy", "provider", "address" },
				null, null, null, null, "timestamp desc");
		if (cursor.moveToFirst()) {
			do {

				
				Location l = new Location(cursor.getString(5));
				l.setTime(cursor.getLong(0));
				l.setLatitude(cursor.getDouble(1));
				l.setLongitude(cursor.getDouble(2));
				l.setAltitude(cursor.getDouble(3));
				l.setAccuracy(cursor.getFloat(4));

				
				
				list.add(new LocationContainer(l, cursor.getString(6)));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		
		return list;
	}

	private static class OpenHelper extends SQLiteOpenHelper {

		OpenHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + TABLE_NAME
					+ "(id INTEGER PRIMARY KEY, timestamp TEXT, lat TEXT, lon TEXT, alt TEXT, accuracy TEXT, provider TEXT, address TEXT)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w("db helper",
					"Upgrading database, this will drop tables and recreate.");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
	}

}
